// pkgUser/pages/profile/profile.js
const app = getApp()
import { formatTime } from '../../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    value: '111',
    showCommonPopup: false,
    placeholder: '',
    title: '',
    maxlength: 0,
    showBirthdaySheet: false,
    showSexSheet: false,
    currentDate: new Date().getTime()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getUserInfo()
  },
  async getUserInfo() {
    const { data: { userInfo } } = await wx.http.get('/user/getUserInfo')
    this.setData({ ...userInfo })
  },
  // 打开公共弹层
  openCommonPopup(ev) {
    if (ev.mark.field === 'username') {
      this.setData({ placeholder: '请输入昵称', title: '编辑昵称', maxlength: 15 })
    } else if (ev.mark.field === 'mobile') {
      this.setData({ placeholder: '请输入手机号', title: '编辑手机号', maxlength: 11 })
    } else if (ev.mark.field === 'address') {
      this.setData({ placeholder: '请输入家庭住址', title: '编辑家庭住址', maxlength: 50 })
    } else if (ev.mark.field === 'notes') {
      this.setData({ placeholder: '请输入个性留言', title: '编辑个性留言', maxlength: 20 })
    } else {
      this.setData({ placeholder: '数据错误，退出重试！', title: '数据错误' })
    }
    this.field = ev.mark.field
    this.setData({ value: this.data[ev.mark.field] })
    this.setData({ showCommonPopup: true })
  },
  //关闭弹层
  closeCommonPopup() {
    this.setData({ showCommonPopup: false })
  },
  onLeftRight() {
    this.closeCommonPopup()
  },
  // 确认修改
  async onClickRight() {
    this.setData({ [this.field]: this.data.value })
    const res = await wx.http.post('/user/update', { id: this.data.id, [this.field]: this.data[this.field] })
    console.log(res)
    this.closeCommonPopup()
  },
  closeBirthdaySheet() {
    this.setData({ showBirthdaySheet: false })
  },
  openBirthdaySheet() {
    this.setData({ showBirthdaySheet: true })
  },
  async selcetBirthdayInfo(ev) {
    this.setData({ birthday: formatTime(new Date(ev.detail), '-') })
    const res = await wx.http.post('/user/update', { id: this.data.id, birthday: this.data.birthday })
    console.log(res)
  },
  openSexSheet() {
    this.setData({ showSexSheet: true })
  },
  closeSexSheet() {
    this.setData({ showSexSheet: false })
  },
  async selcetSexInfo(ev) {
    const sex = ev.detail.name === '男' ? 0 : 1
    this.setData({ sex })
    const res = await wx.http.post('/user/update', { id: this.data.id, sex: this.data.sex })
    console.log(res)
  },
  toCropper() {
    wx.navigateTo({
      url: `../cropper/cropper?imgSrc=http://124.223.14.236:8060/${this.data.icon}`
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    if (app.globalData.imgSrc) {
      this.setData({
        icon: app.globalData.imgSrc
      })
      await wx.http.post('/user/update', { id: this.data.id, icon: this.data.icon })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})